﻿using System;
using System.Web.Mvc;
using iss.sg4210.se18.team6s.jass.BL;
using iss.sg4210.se18.team6s.jass.DAL;
using iss.sg4210.se18.team6s.Utilities;

namespace iss.sg4210.se18.team6s.Controllers
{
    [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
    public class CompanyController : Controller
    {
        CompanyFacade companyFacade = null;
        public CompanyController()
        {
            companyFacade = new CompanyFacade();
        }
        //
        // GET: /Company/

        public ActionResult Index()
        {
            return View(companyFacade.FindAllCompanies());
        }

        //
        // GET: /Company/Details/5

        public ActionResult Details(int id)
        {
            Company company = companyFacade.GetCompany(id);
            return View(company);
        }

        //
        // GET: /Company/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Company/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Company c = new Company();
                UpdateModel(c, collection);
                companyFacade.AddCompany(c);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View();
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /Company/Edit/5

        public ActionResult Edit(int id)
        {
            Company company = companyFacade.GetCompany(id);
            return View(company);
        }

        //
        // POST: /Company/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Company c = companyFacade.GetCompany(id);
            try
            {
                // TODO: Add update logic here
                UpdateModel(c);
                companyFacade.UpdateCompany(c);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(c);
            }
            catch (Exception ex)
            {
                return View(c);
            }
        }

        //
        // GET: /Company/Delete/5

        public ActionResult Delete(int id)
        {
            Company company = companyFacade.GetCompany(id);
            return View(company);
        }

        //
        // POST: /Company/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                companyFacade.DeleteCompany(id);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(companyFacade.GetCompany(id));
            }
            catch (Exception ex)
            {
                return View(companyFacade.GetCompany(id));
            }
        }
    }
}
